<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="seaslog.constants.html">« 预定义常量</a></li>
      <li style="float: right;"><a href="ref.seaslog.html">Seaslog 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.seaslog.html">SeasLog</a></li>
    <li>范例</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="seaslog.examples" class="chapter">
 <h1>范例</h1>


 <div class="section">
  <div class="example" id="example-3508">
    <p><strong>示例 #1 获取与设置根目录</strong></p>
    <div class="example-contents">
  <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$basePath1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">getBasePath</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;</span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">setBasePath</span><span style="color: #007700">(</span><span style="color: #DD0000">'/log/base_test'</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$basePath2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">getBasePath</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$basePath1</span><span style="color: #007700">,</span><span style="color: #0000BB">$basePath2</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
  <div class="cdata"><pre>
  string(12) &quot;/var/log/www&quot;
  string(14) &quot;/log/base_test&quot;
  </pre></div>
    </div>
  </div>
 </div>

<div class="section">
  <div class="example" id="example-3509">
    <p><strong>示例 #2 获取与设置 Logger</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$lastLogger1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">getLastLogger</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">setLogger</span><span style="color: #007700">(</span><span style="color: #DD0000">'testModule/app1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$lastLogger2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">getLastLogger</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$lastLogger1</span><span style="color: #007700">,</span><span style="color: #0000BB">$lastLogger2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(7) &quot;default&quot;
string(15) &quot;testModule/app1&quot;
</pre></div>
    </div>
  </div>
 </div>
 
 <div class="section">
  <div class="example" id="example-3510">
    <p><strong>示例 #3 快速写入日志</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">SEASLOG_ERROR</span><span style="color: #007700">,</span><span style="color: #DD0000">'this&nbsp;is&nbsp;a&nbsp;error&nbsp;test&nbsp;by&nbsp;::log'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">debug</span><span style="color: #007700">(</span><span style="color: #DD0000">'this&nbsp;is&nbsp;a&nbsp;{userName}&nbsp;debug'</span><span style="color: #007700">,array(</span><span style="color: #DD0000">'{userName}'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'neeke'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">info</span><span style="color: #007700">(</span><span style="color: #DD0000">'this&nbsp;is&nbsp;a&nbsp;info&nbsp;log'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">notice</span><span style="color: #007700">(</span><span style="color: #DD0000">'this&nbsp;is&nbsp;a&nbsp;notice&nbsp;log'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">warning</span><span style="color: #007700">(</span><span style="color: #DD0000">'your&nbsp;{website}&nbsp;was&nbsp;down,please&nbsp;{action}&nbsp;it&nbsp;ASAP!'</span><span style="color: #007700">,array(</span><span style="color: #DD0000">'{website}'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'github.com'</span><span style="color: #007700">,</span><span style="color: #DD0000">'{action}'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'rboot'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">error</span><span style="color: #007700">(</span><span style="color: #DD0000">'a&nbsp;error&nbsp;log'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">critical</span><span style="color: #007700">(</span><span style="color: #DD0000">'some&nbsp;thing&nbsp;was&nbsp;critical'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">alert</span><span style="color: #007700">(</span><span style="color: #DD0000">'yes&nbsp;this&nbsp;is&nbsp;a&nbsp;{messageName}'</span><span style="color: #007700">,array(</span><span style="color: #DD0000">'{messageName}'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'alertMSG'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">emergency</span><span style="color: #007700">(</span><span style="color: #DD0000">'Just&nbsp;now,&nbsp;the&nbsp;house&nbsp;next&nbsp;door&nbsp;was&nbsp;completely&nbsp;burnt&nbsp;out!&nbsp;{note}'</span><span style="color: #007700">,array(</span><span style="color: #DD0000">'{note}'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'it`s&nbsp;a&nbsp;joke'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
    日志的默认模板是 <em class="emphasis">seaslog.default_template = &quot;%T | %L | %P | %Q | %t | %M&quot;</em>.
    这意味着，在默认情况下，日志的记录格式是： `{dateTime} | {level} | {pid} | {uniqid} | {timeStamp} | {logInfo}`.
    </p></div>
    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents"><p>
      <em class="emphasis">seaslog.appender = 1</em>
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
2014-07-27 08:53:52 | ERROR | 23625 | 599159975a9ff | 1406422432.786 | this is a error test by log
2014-07-27 08:53:52 | DEBUG | 23625 | 599159975a9ff | 1406422432.786 | this is a neeke debug
2014-07-27 08:53:52 | INFO | 23625 | 599159975a9ff | 1406422432.787 | this is a info log
2014-07-27 08:53:52 | NOTICE | 23625 | 599159975a9ff | 1406422432.787 | this is a notice log
2014-07-27 08:53:52 | WARNING | 23625 | 599159975a9ff | 1406422432.787 | your github.com was down,please rboot it ASAP!
2014-07-27 08:53:52 | ERROR | 23625 | 599159975a9ff | 1406422432.787 | a error log
2014-07-27 08:53:52 | CRITICAL | 23625 | 599159975a9ff | 1406422432.787 | some thing was critical
2014-07-27 08:53:52 | EMERGENCY | 23625 | 599159975a9ff | 1406422432.787 | Just now, the house next door was completely burnt out! it is a joke
</pre></div>
    </div>
    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents"><p>
      <em class="emphasis">seaslog.appender = 2</em> or <em class="emphasis">seaslog.appender = 3</em> 
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
The log style finally formatted such as:
&lt;15&gt;1 2017-08-27T01:24:59+08:00 vagrant-ubuntu-trusty test/logger[27171]: 2016-06-25 00:59:43 | DEBUG | 21423 | 599157af4e937 | 1466787583.322 | this is a neeke debug
&lt;14&gt;1 2017-08-27T01:24:59+08:00 vagrant-ubuntu-trusty test/logger[27171]: 2016-06-25 00:59:43 | INFO | 21423 | 599157af4e937 | 1466787583.323 | this is a info log
&lt;13&gt;1 2017-08-27T01:24:59+08:00 vagrant-ubuntu-trusty test/logger[27171]: 2016-06-25 00:59:43 | NOTICE | 21423 | 599157af4e937 | 1466787583.324 | this is a notice log
</pre></div>
    </div>
  </div>
 </div>

 <div class="section">
  <div class="example" id="example-3511">
    <p><strong>示例 #4 快速获取某级别下日志的数量</strong></p>
    <div class="example-contents"><p>
    <em class="emphasis">SeasLog</em> 通过系统命令管道调用 `grep -wc` 获取数量，并返回给 PHP (返回数组或单个数值).
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$countResult1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">analyzerCount</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$countResult2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">analyzerCount</span><span style="color: #007700">(</span><span style="color: #0000BB">SEASLOG_WARNING</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$countResult3&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">analyzerCount</span><span style="color: #007700">(</span><span style="color: #0000BB">SEASLOG_ERROR</span><span style="color: #007700">,</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'Ymd'</span><span style="color: #007700">,</span><span style="color: #0000BB">time</span><span style="color: #007700">()));<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$countResult1</span><span style="color: #007700">,</span><span style="color: #0000BB">$countResult2</span><span style="color: #007700">,</span><span style="color: #0000BB">$countResult3</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(8) {
  [&quot;DEBUG&quot;]=&gt;
  int(3)
  [&quot;INFO&quot;]=&gt;
  int(3)
  [&quot;NOTICE&quot;]=&gt;
  int(3)
  [&quot;WARNING&quot;]=&gt;
  int(3)
  [&quot;ERROR&quot;]=&gt;
  int(6)
  [&quot;CRITICAL&quot;]=&gt;
  int(3)
  [&quot;ALERT&quot;]=&gt;
  int(3)
  [&quot;EMERGENCY&quot;]=&gt;
  int(3)
}
int(7)
int(1)
</pre></div>
    </div>
  </div>
 </div>

 <div class="section">
  <div class="example" id="example-3512">
    <p><strong>示例 #5 获取某级别下日志的详情</strong></p>
    <div class="example-contents"><p>
    <em class="emphasis">SeasLog</em> 通过系统命令管道调用 `grep -w` 获取日志详情，并返回数组给 PHP.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$detailErrorArray&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">analyzerDetail</span><span style="color: #007700">(</span><span style="color: #0000BB">SEASLOG_ERROR</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$detailErrorArray</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">SeasLog</span><span style="color: #007700">::</span><span style="color: #0000BB">analyzerDetail</span><span style="color: #007700">(</span><span style="color: #0000BB">SEASLOG_ERROR</span><span style="color: #007700">,</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'Ymd'</span><span style="color: #007700">,</span><span style="color: #0000BB">time</span><span style="color: #007700">())));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(6) {
  [0] =&gt;
  string(83) &quot;2014-02-24 00:14:02 | ERROR | 8568 | 599157af4e937 | 1393172042.717 | test error 3 &quot;
  [1] =&gt;
  string(83) &quot;2014-02-24 00:14:04 | ERROR | 8594 | 5991576584446 | 1393172044.104 | test error 3 &quot;
  [2] =&gt;
  string(83) &quot;2014-02-24 00:14:04 | ERROR | 8620 | 1502697015147 | 1393172044.862 | test error 3 &quot;
  [3] =&gt;
  string(83) &quot;2014-02-24 00:14:05 | ERROR | 8646 | 599159975a9ff | 1393172045.989 | test error 3 &quot;
  [4] =&gt;
  string(83) &quot;2014-02-24 00:14:07 | ERROR | 8672 | 599159986ec28 | 1393172047.882 | test error 3 &quot;
  [5] =&gt;
  string(83) &quot;2014-02-24 00:14:08 | ERROR | 8698 | 5991599981cec | 1393172048.736 | test error 3 &quot;
}

array(2) {
  [0] =&gt;
  string(83) &quot;2014-02-24 00:14:02 | ERROR | 8568 | 599157af4e937 | 1393172042.717 | test error 3 &quot;
  [1] =&gt;
  string(83) &quot;2014-02-24 00:14:04 | ERROR | 8594 | 5991576584446 | 1393172044.104 | test error 3 &quot;
}
</pre></div>
    </div>
  </div>
 </div>

</div>
</div></div></body></html>